﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using BingleMaps.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace BingleMaps.Views
{
    /// <summary>
    /// Description for SettingsPage.
    /// </summary>
    public partial class SettingsPage
    {
        private readonly SettingsViewModel _settings;
        private readonly Binding
            _mapSourceBinding = new Binding("MapSource") { Mode = BindingMode.TwoWay },
            _distanceUnitBinding = new Binding("DistanceUnit") { Mode = BindingMode.TwoWay, Converter = (IValueConverter)Application.Current.Resources["DistanceUnitConverter"] };
        public SettingsPage()
        {
            InitializeComponent();
            _settings = (SettingsViewModel)DataContext;
            MapSourceListPicker.SetBinding(ListPicker.SelectedItemProperty, _mapSourceBinding);
            DistanceUnitListPicker.SetBinding(ListPicker.SelectedItemProperty, _distanceUnitBinding);
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (NavigationService != null && NavigationService.CanGoBack)
                base.OnBackKeyPress(e);
            else e.Cancel = true;
        }

        private void SyncToggleSwitchChecked(object sender, RoutedEventArgs e)
        {
            if (_settings.DropNetUserLogin == null)
                NavigationService.Navigate(new Uri("/Views/DropBoxView.xaml", UriKind.Relative));
        }

        private void SyncToggleSwitchUnchecked(object sender, RoutedEventArgs e)
        {
            _settings.DropNetUserLogin = null;
        }

        private void EmailHyperlinkButtonClick(object sender, RoutedEventArgs e)
        {
            new EmailComposeTask { To = Application.Current.Resources["Email"].ToString(), Subject = Application.Current.Resources["EmailSubject"].ToString() }.Show();
        }

        private void WebsiteHyperlinkButtonClick(object sender, RoutedEventArgs e)
        {
            new WebBrowserTask { URL = Application.Current.Resources["Website"].ToString() }.Show();
        }
    }
}